#include "Airplane.h"

Airplane::Airplane(int airplaneNumInput)
{
    airplaneNum = airplaneNumInput;
}

void Airplane::updatePosition(Edge &curEdge)
{
    totalTime++;
    if(edgeTravel.size() == 0) return;
    if(state == TRAVELING)
    {
        edgeLoc += 1;
    }

    //See if the plane SHOULD take off if it hasn't.
    if(!curEdge.airplaneDepatureQueue.empty())
    {
        vector<Airplane*> curAirplanes = curEdge.airplanes;
        if(state == STANDBY && curEdge.airplaneDepatureQueue[0].airplaneNum == airplaneNum)
        {
            if(curEdge.airplanes.size() != 0)
            {
                //Look at the last airplane that took off and see if it is far enough away to takeoff.
                double nextAirplaneLoc = curAirplanes[curEdge.airplanes.size() - 1]->edgeLoc;
                if(nextAirplaneLoc > milesInTow)
                {
                    //Set the airplane to traveling, add the airplane to the edge, and remove it from the departure queue.
                    state = TRAVELING;
                    curEdge.airplanes.push_back(&(*this));
                    curEdge.airplaneDepatureQueue.erase(curEdge.airplaneDepatureQueue.begin());
                }
            }
            else
            {
                //Set the airplane to traveling, add the airplane to the edge, and remove it from the departure queue.
                state = TRAVELING;
                curEdge.airplanes.push_back(&(*this));
                curEdge.airplaneDepatureQueue.erase(curEdge.airplaneDepatureQueue.begin());
            }
        }

    }
}

vector<int> Airplane::getActions(vector<Airport> airports, vector<vector<vector<vector<int> > > > lookupTable)
{
    vector<int> possibleActions;
    for(unsigned int i = 0; i < lookupTable[currentAirport][arrivalAirport].size(); i++)
    {
        //Find route to next airport in hop.
        vector<int> route = lookupTable[currentAirport][arrivalAirport][i];
        //Find congestion in next airport in hop.
        int congestion = airports[route[0]].planesInAirspace;
        if(congestion >= airports[route[0]].planesInAirspaceMax) continue;
        if(i == 0) possibleActions.push_back(RouteFirst);
        if(i == 1) possibleActions.push_back(RouteSecond);
        if(i == 2) possibleActions.push_back(RouteThird);
    }
    possibleActions.push_back(Delay);
    return possibleActions;
}

void Airplane::takeAction(Actions a, vector<vector<vector<vector<int> > > > lookupTable)
{
    if(a == RouteFirst) edgeTravel = lookupTable[currentAirport][arrivalAirport][0];
    if(a == RouteSecond) edgeTravel = lookupTable[currentAirport][arrivalAirport][1];
    if(a == RouteThird) edgeTravel = lookupTable[currentAirport][arrivalAirport][2];
    if(a == Delay) edgeTravel.clear();
}
Airplane::~Airplane()
{
    //dtor
}
